package com.zkzx.company.api.controller.area;

import com.zkzx.common.area.AreaVO;
import com.zkzx.company.api.mapper.entity.Area;
import com.zkzx.company.api.service.area.AreaService;
import com.zkzx.util.map.AMapUtil;
import com.zkzx.util.map.vo.AMapPoiVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags="国家行政区划")
@RequestMapping(value = "/area")
public class AreaController {

    @Resource
    private AreaService areaService;

    @Resource
    private AMapUtil aMapUtil;

    @GetMapping("/{pid}/{level}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取国家行政区划", notes = "获取国家行政区划")
    public List<AreaVO> getArea(@ApiParam(value = "父级区划Code") @PathVariable(value = "pid") Integer pid,
                                @ApiParam(value = "行政区划等级")  @PathVariable(value = "level") String level) {
        log.error("获取国家行政区划API 入参 id:{} level:{}", pid, level);

        return  areaService.getArea(pid, level);
    }

    @GetMapping("/getLikeAreaName/{name}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "[匹配中文区划名称]", notes = "匹配中文区划名称")
    public List<AreaVO> getLikeAreaName(@ApiParam(value = "中文区划名称")  @PathVariable(value = "name") String name) {
        log.error("匹配中文区划名称API 入参 name:{}", name);

        return  areaService.getLikeAreaName(name);
    }

    /** 一次性代码,存档备用;
     * 行政区划名称通过高德API清洗初始坐标点数据,默认取第一条地图数据;
     */
//    @GetMapping("/location/initialization")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取国家行政区划", notes = "获取国家行政区划")
    public void location() {
        log.error("国家行政区划 location initialization");

        List<Area> areaAll = areaService.getAreaAll();
        log.error("国家行政区划 location initialization areaAll.size() {}", areaAll.size());

        areaAll.forEach(area -> {
            try {
                List<AMapPoiVO> aMapPoiVOs = aMapUtil.getKeywordsAddByLbs(area.getName(), "");

                AMapPoiVO aMapPoiVO = aMapPoiVOs.get(0);
                log.error(aMapPoiVOs.get(0).toString());

                String location = aMapPoiVO.getLocation();

                area.setLocation(location);

                areaService.updateArea(area);
            } catch (Exception e) {
               log.error("通过高德API获取行政区初始坐标失败", e.fillInStackTrace());
            }
        });
    }
}
